package com.itboy.mall.pms.attrattrgroup.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import com.itboy.mall.pms.attr.domain.MallAttr;
import com.itboy.mall.pms.attr.service.IMallAttrService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.pms.attrattrgroup.bo.MallAttrAttrGroupAddBo;
import com.itboy.mall.pms.attrattrgroup.bo.MallAttrAttrGroupQueryBo;
import com.itboy.mall.pms.attrattrgroup.bo.MallAttrAttrGroupEditBo;
import com.itboy.mall.pms.attrattrgroup.domain.MallAttrAttrGroup;
import com.itboy.mall.pms.attrattrgroup.mapper.MallAttrAttrGroupMapper;
import com.itboy.mall.pms.attrattrgroup.vo.MallAttrAttrGroupVo;
import com.itboy.mall.pms.attrattrgroup.service.IMallAttrAttrGroupService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 属性&属性分组关联Service业务层处理
 *
 * @author itboy
 * @date 2021-07-24
 */
@Service
public class MallAttrAttrAttrGroupServiceImpl extends ServicePlusImpl<MallAttrAttrGroupMapper, MallAttrAttrGroup> implements IMallAttrAttrGroupService {


	@Autowired
	private IMallAttrService iMallAttrService;
    @Override
    public MallAttrAttrGroupVo queryById(Long id){
        return getVoById(id, MallAttrAttrGroupVo.class);
    }

    @Override
    public TableDataInfo<MallAttrAttrGroupVo> queryPageList(MallAttrAttrGroupQueryBo bo) {
        PagePlus<MallAttrAttrGroup, MallAttrAttrGroupVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallAttrAttrGroupVo.class);
		List<MallAttrAttrGroupVo> recordsVo = result.getRecordsVo();
		if(CollectionUtil.isNotEmpty(recordsVo)){
			//属性ids
			List<Long> attrIdList = recordsVo.stream().map(e -> e.getAttrId()).collect(Collectors.toList());
			LambdaQueryWrapper<MallAttr> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.in(MallAttr::getAttrId,attrIdList);
			queryWrapper.like(StringUtils.isNotBlank(bo.getAttrName()),MallAttr::getAttrName,bo.getAttrName());
			//查询属性
			List<MallAttr> mallAttrList = iMallAttrService.list(queryWrapper);

			recordsVo.forEach(e->{
				//过滤属性
				MallAttr mallAttr = mallAttrList.stream().filter(t -> t.getAttrId().equals(e.getAttrId())).findFirst().orElse(null);
				if(ObjectUtil.isNotNull(mallAttr)){
					e.setAttrName(mallAttr.getAttrName());
					e.setAttrType(mallAttr.getAttrType());
					if(StringUtils.isNotBlank(mallAttr.getValueSelect())){
						List<String> list = Arrays.asList(mallAttr.getValueSelect().split(","));
						e.setValueSelect(list.toArray(new String[list.size()]));
					}
				}
			});
			if(CollectionUtil.isNotEmpty(recordsVo)){
				List<MallAttrAttrGroupVo> collect = recordsVo.stream().filter(e -> StringUtils.isNotBlank(e.getAttrName())).collect(Collectors.toList());
				recordsVo=collect;
			}
		}

		return PageUtils.buildDataInfo(recordsVo);
    }

    @Override
    public List<MallAttrAttrGroupVo> queryList(MallAttrAttrGroupQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallAttrAttrGroupVo.class);
    }

    private LambdaQueryWrapper<MallAttrAttrGroup> buildQueryWrapper(MallAttrAttrGroupQueryBo bo) {
        LambdaQueryWrapper<MallAttrAttrGroup> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAttrGroupId() != null, MallAttrAttrGroup::getAttrGroupId, bo.getAttrGroupId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallAttrAttrGroupAddBo bo) {
    	List<MallAttrAttrGroup> mallAttrAttrGroupList = new ArrayList<>();
		 Long[] attrIds = bo.getAttrId();
		for (Long attrId : attrIds) {
			MallAttrAttrGroup mallAttrAttrGroup = new MallAttrAttrGroup();
			mallAttrAttrGroup.setAttrGroupId(bo.getAttrGroupId());
			mallAttrAttrGroup.setAttrId(attrId);
			mallAttrAttrGroupList.add(mallAttrAttrGroup);
		}
        return saveAll(mallAttrAttrGroupList);
    }

    @Override
    public Boolean updateByEditBo(MallAttrAttrGroupEditBo bo) {
        MallAttrAttrGroup update = BeanUtil.toBean(bo, MallAttrAttrGroup.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallAttrAttrGroup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
